import dayjs from 'dayjs'

// 当前零点
let currentDay = () => {
    let time = new Date().getTime()
    let date = new Date(time)
    let hour = date.getHours()
    let minutes = date.getMinutes()
    let seconds = date.getSeconds()
    let mills = date.getMilliseconds()
    time = time - hour * 60 * 60 * 1000 - minutes * 60 * 1000 - seconds * 1000 - mills
    return time
}

/**
 * 格式化时间戳（秒|毫秒）
 * @param {timestamp} value
 */
const timeFilter = value => {
    value = value.toString()
    if (value) {
        if (value.length === 17) {
            value = value.substr(0, 13)
        }
        if (value.length === 13) {
            return dayjs(Number(value)).format('YYYY-MM-DD HH:mm:ss')
        }
        return dayjs.unix(Number(value)).format('YYYY-MM-DD HH:mm:ss')
    } else {
        return '-'
    }
}

/**
 * 当前会话列表的 聊天的时间显示
 * @param {*} time
 */
const transformTime = (time) => {
    let nt = Number(time)
    let date = new Date(nt)
    let h = date.getHours()
    let m = date.getMinutes()
    if (time > currentDay) {
        if (m < 10) {
            return (h + ':0' + m)
        } else {
            return (h + ':' + m)
        }
    } else if (time > currentDay - 24 * 60 * 60 * 1000) {
        return '昨天'
    } else {
        let month = date.getMonth() + 1
        let day = date.getDate()
        return `${month}月${day}日`
    }
}

/**
 * 返回时间 精确到分
 * @param {*} YYYY-MM-DD HH:mm
 */
const transformTime2Minute = (time) => {
    var date = new Date(time)
    let Y = date.getFullYear() + '-'
    let M =
        (date.getMonth() + 1 < 10
            ? '0' + (date.getMonth() + 1)
            : date.getMonth() + 1) + '-'
    let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
    let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
    let m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
    // let s = date.getSeconds()
    let nt = Y + M + D + h + m
    return nt
}

/**
 * 手机号格式化
 * @param {String} phone
 */
const formatPhone = phone => {
    phone = phone.toString()
    return phone.substr(0, 3) + '****' + phone.substr(7, 11)
}

/**
 * 4位一空格（格式化银行卡）
 * @param {String} val
 */
const formatBank = val => {
    if (val) {
        return val
            .toString()
            .replace(/\s/g, '')
            .replace(/(.{4})/g, '$1 ')
    }
}

/**
 * 千分位格式化
 * @param {数字} val
 */
const toThousands = val => {
    let num = (val || 0).toString(),
        result = ''
    while (num.length > 3) {
        result = ',' + num.slice(-3) + result
        num = num.slice(0, num.length - 3)
    }
    if (num) {
        result = num + result
    }
    return result
}

/**
 * 格式化小数位
 * @param val 传入的值
 * @param pos 保留的小数位
 * @returns {*}
 */
const formatFloat = (val, pos = 2) => {
    let f = parseFloat(val)
    if (isNaN(f)) {
        return false
    }
    f = Math.round(val * Math.pow(10, pos)) / Math.pow(10, pos) // pow 幂
    let s = f.toString()
    let rs = s.indexOf('.')
    if (rs < 0) {
        rs = s.length
        s += '.'
    }
    while (s.length <= rs + pos) {
        s += '0'
    }
    return s
}

/**
 * 格式化时长
 * @param second
 * @returns {string}
 */
const realFormatSecond = second => {
    let secondType = typeof second

    if (secondType === 'number' || secondType === 'string') {
        second = parseInt(second)

        let hours = Math.floor(second / 3600)
        second = second - hours * 3600
        let mimute = Math.floor(second / 60)
        second = second - mimute * 60

        return (
            hours +
            ':' +
            ('0' + mimute).slice(-2) +
            ':' +
            ('0' + second).slice(-2)
        )
    } else {
        return '0:00:00'
    }
}

// ========================================

// 发朋友圈|群发|小工具 页面的子路由对应的title
const titleFilter = (routeName) => {
    let titleName = {
        circleSend: '发朋友圈',
        circleMy: '我的朋友圈',
        circleLog: '朋友圈日志',
        groupSend: '群发助手',
        groupLog: '群发日志',
        newFriend: '新的朋友',
        addFriend: '添加好友',
        addFriendAuto: '批量加好友',
        searchPhones: '加好友结果查询',
        searchNumber: '通过号码查询微信号',
        autoHi: '自动打招呼',
        qrCode: '微信二维码',
        autoTask: '自动任务',
        autoReply: '自动回复',
        clearFans: '清理僵尸粉',
        phoneManagement: '手机管理',
        moreTools: '更多功能',
        takeLabels: '批量打标签',
        friendsFilter: '好友筛选',
    }
    return titleName[routeName] ? titleName[routeName] : routeName
}

// 从红包key中获取发送者
const hbUrlFilter = (hbUrl) => {
    if (hbUrl) {
        let reg = /&sendusername=.+?&/
        let result = hbUrl.match(reg)[0]
        return result.replace('&sendusername=', '').replace('&', '')
    }
}



// 根据labelId获取labelName
const labelFilter = (labelId, labels) => {
    for (const label of labels) {
        if (label.LabelId === Number(labelId)) {
            return label.LabelName
        }
    }
    return labelId
}

/**
 * 获取文件名
 * @param {string} file
 */
const getFileName = (file) => {
    let name = file
    try {
        name = JSON.parse(file).name
        return name
    } catch (error) {
        return name.replace(/.*\//, '')
    }
}

/**
 * 返回好友来源
 * @param {*} sourceId
 */
const getFriendSource = (sourceId) => {
    let sourceMap = {
        1: '通过搜索QQ号添加',
        1000001: '对方通过搜索QQ号添加',
        3: '通过搜索微信号添加',
        1000003: '对方通过搜索微信号添加',
        4: "来自QQ好友",
        8: '通过群聊添加',
        1000008: '对方通过群聊添加',
        1000014: '对方通过群聊添加',
        10: "通过手机通讯录添加",
        1000010: "对方通过手机通讯录添加",
        1000013: "对方通过手机通讯录添加",
        15: "通过搜索手机号添加",
        1000015: "对方通过搜索手机号添加",
        17: "通过名片分享添加",
        1000017: "对方通过名片分享添加",
        18: "通过附近的人添加",
        1000018: "对方通过附近的人添加",
        22: "通过摇一摇添加",
        1000022: "对方通过摇一摇添加",
        25: "通过漂流瓶添加",
        1000025: "对方通过漂流瓶添加",
        30: "通过扫一扫添加",
        1000030: "对方通过扫一扫添加",
        34: "公众号",
        48: "雷达",
        58: "通过Google联系人添加",
        76: "通过LinkedIn添加",
    }
    if (sourceId && sourceMap[sourceId]) {
        return sourceMap[sourceId]
    }
    return '未知'
}


export default {
    timeFilter,
    transformTime,
    transformTime2Minute,
    formatPhone,
    formatBank,
    toThousands,
    formatFloat,
    realFormatSecond,
    titleFilter,
    hbUrlFilter,
    labelFilter,
    getFileName,
    getFriendSource
}
